//
//  ViewController.m
//  08-喜马拉雅
//
//  Created by wangneihan on 15/12/16.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self loadMainView];
    [self loadTopBar];
    }

-(void)loadMainView{
    
    UIScrollView *mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    mainView.contentInset = UIEdgeInsetsMake(60, 0, 60, 0);
    
    mainView.scrollEnabled = YES;
    
    //mainView.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:mainView];
    
    CGFloat width = 300;
    
    CGFloat btnViewMarginX = (self.view.frame.size.width - 300)/2;
    
    CGFloat height = width*0.6;
    
    //Banner
    UIScrollView *scrImgView = [[UIScrollView alloc] initWithFrame:CGRectMake(btnViewMarginX, 0, width, height)];
    
    scrImgView.backgroundColor = [UIColor redColor];
    
    [mainView addSubview:scrImgView];
    
    //Buttons
    NSArray *imgArray = [[NSArray alloc]initWithObjects:@"finditem_ad",
                         @"finditem_hotpeople",
                         @"finditem_hotsound",
                         @"finditem_newpeople",
                         @"finditem_newsound",
                         @"finditem_wallspoint",
                         @"finditem_iwannabehere",nil];
    //,@"finditem_iwannabehere"
    
    CGFloat countHeight = 0;
    
    for (int x = 0; x < imgArray.count; x++) {
        
        UIImage *img = [UIImage imageNamed:imgArray[x]];
        
        int row = x/2;
        
        int col = x%2;
        
        CGFloat baseMarginY = height;
        
        CGFloat marginX = btnViewMarginX + col * (img.size.width + self.view.frame.size.width - 2 * (img.size.width + btnViewMarginX));
        
        CGFloat marginY = baseMarginY + 20 + ([UIImage imageNamed:imgArray[0]].size.height + 20) * row;
        
        NSLog(@"img.size.height : %f  , y : %f  ,row : %d",img.size.height , (img.size.height + 20) * row, row);
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(marginX, marginY, img.size.width, img.size.height)];
        
        [btn setBackgroundImage:img forState:UIControlStateNormal];
        
        [mainView addSubview:btn];
        
        if(x == (imgArray.count-1))
        {
            countHeight = btn.frame.origin.y + img.size.height;
        }
        
    }
    
    mainView.contentSize = CGSizeMake(self.view.frame.size.width, countHeight);
    
    mainView.showsHorizontalScrollIndicator = NO;
    
    mainView.showsVerticalScrollIndicator = NO;
   
}

-(void)loadTopBar
{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    
    topView.backgroundColor = [UIColor whiteColor];
    
    topView.alpha = 0.9;
    
    [self.view addSubview:topView];
    
    [self.view bringSubviewToFront:topView];
}

-(void)loadBottomBar
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
